<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog = true"
                >新增角色</el-button
              >
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="{ row }">
                  <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="editRole(row.id)"
                    >编辑</el-button
                  >
                  <el-button size="small" type="danger" @click="deleteRole(row)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination layout="prev,pager,next" />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <el-dialog title="编辑部门" :visible="showDialog" @close="btnCancel">
      <el-form
        ref="roleForm"
        :model="roleForm"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item prop="name" label="角色名称">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 放置footer插槽 -->
      <el-row type="flex" justify="center">
        <el-col :span="8">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel"
     >
      <el-tree
      ref="permTree"
      :data="permData"
      :props="defaultProps"
       show-checkbox
       node-key="id"
       check-strictly
       default-expand-all
       :default-checked-keys="selectCheck"
      ></el-tree>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
          <el-button size="small" @click="btnPermCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import {
  _getRoleList,
  _deleteRole,
  _addRole,
  _getRoleDetail,
  _updateRole,
  _assignPerm,
} from '@/api/setting'
import { tranListToTreeData } from '@/utils'
import { _getPermissionList } from '@/api/permission'
export default {
  data() {
    return {
      page: {
        page: 1,
        pagesize: 10,
        total: 0, // 记录总数
      },
      list: null, // 员工数据
      roleForm: {
        name: '',
        description: '',
      },
      showDialog: false,
      showPermDialog: false,
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' },
        ],
      },
      permData: [], // 接收权限数据
      defaultProps: {
        label: 'name'
      },
      roleId: null, // 用来记录当前分配权限的id
      selectCheck: [] // 用来记录当前的权限点的标识
    }
  },
  methods: {
    btnPermOK() {
      _assignPerm({
        permIds: this.$refs.permTree.getCheckedKeys(), 
        id: this.roleId}
        ).then(res => {
        console.log(res)
        this.initRoleList()
      })
    },
    async assignPerm(id) {
      this.permData = tranListToTreeData(await _getPermissionList(), '0') // 转化list到树形数据
      this.roleId = id
      const { permIds } = await _getRoleDetail(id)
      console.log(permIds)
      this.selectCheck = permIds // 将当前角色所拥有的权限id赋值
      this.showPermDialog = true
    },
    deleteRole(item) {
      console.log(item)
      _deleteRole(item.id).then((res) => {
        this.initRoleList()
      })
    },
    async editRole(id) {
      this.roleForm = await _getRoleDetail(id)
      this.showDialog = true
    },
    btnPermCancel() {
      this.selectCheck = [] // 重置数据
      this.showPermDialog = false
    },
    btnCancel() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.showDialog = false
      // 移除校验
      this.$refs.roleForm.resetFields()
    },
    async btnOK() {
      try {
        await this.$refs.roleForm.validate()
        if (this.roleForm.id) {
          await _updateRole(this.roleForm).then((res) => {
            this.$message.success('编辑成功')
            this.showDialog = false
          })
        } else {
          await _addRole(this.roleForm).then((res) => {
            this.initRoleList()
            this.$message.success('添加成功')
            this.showDialog = false
          })
        }
        this.initRoleList()
      } catch (error) {
        console.log(error)
      }
    },
    async initRoleList() {
      _getRoleList(this.page).then((res) => {
        this.list = res.rows
        console.log(this.list)
      })
    },
  },
  created() {
    this.initRoleList()
  }
}
</script>

<style lang='scss' scopde>
</style>